package com.aiti.lulian.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.*;
import com.aiti.lulian.entity.CardRichText;
import com.aiti.lulian.entity.CardZfFile;
import com.aiti.lulian.entity.LuLianQualification;
import com.aiti.lulian.entity.crad.*;
import com.aiti.lulian.entity.crad.cluster.CardClusterEnterpriseBinding;
import com.aiti.lulian.entity.crad.cluster.CardClusterInfo;
import com.aiti.lulian.entity.crad.cluster.CardZfFloorText;
import com.aiti.lulian.mapper.card.CardEnterpriseFloorMapper;
import com.aiti.lulian.service.*;
import com.aiti.lulian.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class CardEnterpriseFloorServiceImpl extends BaseSerivceImpl<CardEnterpriseFloorMapper, CardEnterpriseFloor> implements ICardEnterpriseFloorService {

    private final CardEnterpriseFloorMapper cardEnterpriseFloorMapper;

    private final ICardEnterpriseFloorMediaService cardEnterpriseFloorMediaService;

    private final ICardEnterpriseFloorAttachService cardEnterpriseFloorAttachService;

    private final ICardEnterpriseFloorLinkService cardEnterpriseFloorLinkService;

    private final ILuLianQualificationService luLianQualificationService;

    private final ILuLianProductService luLianProductService;

    private final CardRichTextService cardRichTextService;

    private final ICardZfFloorTextService cardZfFloorTextService;

    private final ICardClusterInfoService cardClusterInfoService;

    private final ICardClusterEnterpriseBindingService cardClusterEnterpriseBindingService;

    private final ICardZfFileService cardZfFileService;

    private final ILuLianEnterpriseExtensionService luLianEnterpriseExtensionService;

    private final ILuLianEnterpriseService luLianEnterpriseService;

    private final ICardEnterpriseFloorDeptService cardEnterpriseFloorDeptService;

    @Override
    public CardEnterpriseFloorVo getFloorById(String baseId) {
        return cardEnterpriseFloorMapper.selectFloorById(baseId);
    }

    @Override
    public List<CardEnterpriseFloorVo> getFloorList(CardEnterpriseFloorDto cardEnterpriseFloorDto, Page<CardEnterpriseFloor> page) {
        List<CardEnterpriseFloorVo> cardEnterpriseFloorVos = cardEnterpriseFloorMapper.selectFloorList(cardEnterpriseFloorDto, page);
        return cardEnterpriseFloorVos;
    }

    @Override
    public Integer getFloorSort(String enterpriseId) {
        List<CardEnterpriseFloor> cardEnterpriseFloors = cardEnterpriseFloorMapper.selectList(
                new LambdaQueryWrapper<CardEnterpriseFloor>()
                        .eq(CardEnterpriseFloor::getEnterpriseId, enterpriseId)
                        .orderByDesc(CardEnterpriseFloor::getSort)
        );
        if (cardEnterpriseFloors.size() > 0) {
            return cardEnterpriseFloors.get(0).getSort();
        } else {
//            //默认第一个自定义楼层的序号是4，所以返回3，在存储新增的楼层时再加1
//            return 3;
            //默认第一个自定义楼层的序号是3，所以返回2，在存储新增的楼层时再加1
            return 2;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message moveFloor(CardEnterpriseFloorDto cardEnterpriseFloorDto) {
        CardEnterpriseFloor currentFloor = new CardEnterpriseFloor();
        BeanUtils.copyProperties(cardEnterpriseFloorDto, currentFloor);
        List<CardEnterpriseFloor> cardEnterpriseFloors = new ArrayList<>();
        if (StrUtil.equals(cardEnterpriseFloorDto.getMovingDirection(), "1")) {
            //上移楼层
            if (cardEnterpriseFloorDto.getSort() == 3) {
                return Message.fail("不可继续上移");
            } else {
                CardEnterpriseFloor upFloor = this.getOne(new LambdaQueryWrapper<CardEnterpriseFloor>()
                        .select(CardEnterpriseFloor::getBaseId, CardEnterpriseFloor::getSort)
                        .eq(CardEnterpriseFloor::getEnterpriseId, currentFloor.getEnterpriseId())
                        .eq(CardEnterpriseFloor::getSort, cardEnterpriseFloorDto.getSort() - 1));
                upFloor.setSort(upFloor.getSort() + 1);
                cardEnterpriseFloors.add(upFloor);
                currentFloor.setSort(currentFloor.getSort() - 1);
                cardEnterpriseFloors.add(currentFloor);
            }
        } else {
            CardEnterpriseFloor downFloor = this.getOne(new LambdaQueryWrapper<CardEnterpriseFloor>()
                    .select(CardEnterpriseFloor::getBaseId, CardEnterpriseFloor::getSort)
                    .eq(CardEnterpriseFloor::getEnterpriseId, currentFloor.getEnterpriseId())
                    .eq(CardEnterpriseFloor::getSort, cardEnterpriseFloorDto.getSort() + 1));
            downFloor.setSort(downFloor.getSort() - 1);
            cardEnterpriseFloors.add(downFloor);
            currentFloor.setSort(currentFloor.getSort() + 1);
            cardEnterpriseFloors.add(currentFloor);
        }
        boolean move = this.updateBatchById(cardEnterpriseFloors);
        if (move) {
            return Message.success();
        } else {
            return Message.fail();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addMediaFloor(CardEnterpriseFloorMediaDto mediaFloor) {
        String userId = mediaFloor.getUserId();
        CardEnterpriseFloor floor = mediaFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean save = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        List<CardEnterpriseFloorMedia> mediaFloors = mediaFloor.getMediaFloorList();
        if (mediaFloors != null && mediaFloors.size() > 0) {
            List<CardEnterpriseFloorMedia> mediaFloorList = mediaFloors.stream().peek(i -> {
                i.setType(floor.getType());
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardEnterpriseFloorMediaService.saveBatch(mediaFloorList);
            if (! saveBatch) {
                save = false;
            }
        }
        if (save) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addAttachFloor(CardEnterpriseFloorAttachDto attachFloor) {
        String userId = attachFloor.getUserId();
        CardEnterpriseFloor floor = attachFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean save = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        List<CardEnterpriseFloorAttach> attachFloors = attachFloor.getAttachFloorList();
        if (attachFloors != null && attachFloors.size() > 0) {
            List<CardEnterpriseFloorAttach> attachFloorList = attachFloor.getAttachFloorList().stream().peek(i -> {
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardEnterpriseFloorAttachService.saveBatch(attachFloorList);
            if (! saveBatch) {
                save = false;
            }
        }
        if (save) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addOrUpdateLinkFloor(CardEnterpriseFloorLinkDto linkFloor) {
        String userId = linkFloor.getUserId();
        CardEnterpriseFloor floor = linkFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        if (StrUtil.isBlank(floor.getBaseId())) {
            //新增
            floor.setSort(cardEnterpriseFloorSort + 1);
            floor.setCreator(userId);
        } else {
            //更新
            floor.setUpdateBy(userId);
        }
        boolean saveOrUpdate = this.saveOrUpdate(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        cardEnterpriseFloorLinkService.remove(new LambdaQueryWrapper<CardEnterpriseFloorLink>().eq(CardEnterpriseFloorLink::getFloorId, floorId));
        List<CardEnterpriseFloorLink> linkFloors = linkFloor.getLinkFloorList();
        if (linkFloors != null && linkFloors.size() > 0) {
            List<CardEnterpriseFloorLink> linkFloorList = linkFloor.getLinkFloorList().stream().peek(i -> {
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardEnterpriseFloorLinkService.saveBatch(linkFloorList);
            if (! saveBatch) {
                saveOrUpdate = false;
            }
        }
        if (saveOrUpdate) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addQualificationFloor(CardEnterpriseFloorQualificationDto qualificationFloor) {
        String userId = qualificationFloor.getUserId();
        CardEnterpriseFloor floor = qualificationFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean save = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        List<LuLianQualification> qualificationFloors = qualificationFloor.getQualificationFloorList();
        if (qualificationFloors != null && qualificationFloors.size() > 0) {
            List<LuLianQualification> qualificationFloorList = qualificationFloor.getQualificationFloorList().stream().peek(i -> {
                i.setEnterpriseId(enterpriseId);
                i.setType("1");
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = luLianQualificationService.saveBatch(qualificationFloorList);
            if (! saveBatch) {
                save = false;
            }
        }
        if (save) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addRichTextFloor(CardEnterpriseFloorRichTextDto richTextFloor) {
        String userId = richTextFloor.getUserId();
        CardEnterpriseFloor floor = richTextFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean saveFloor = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        CardRichText richText = richTextFloor.getRichTextFloor();
        richText.setTitle(floor.getFloorTitle());
        richText.setCreator(userId);
        richText.setEnterpriseId(enterpriseId);
        richText.setFloorId(floorId);
        if (StrUtil.equals(floor.getType(), "11")) {
            // 复合类楼层
            richText.setType("2");
        }
        boolean saveRichText = cardRichTextService.save(richText);
        if (saveFloor && saveRichText) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addZfTextFloor(CardEnterpriseFloorZfTextDto zfTextFloor) {
        String userId = zfTextFloor.getUserId();
        CardEnterpriseFloor floor = zfTextFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean saveFloor = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        CardZfFloorText zfText = zfTextFloor.getZfTextFloor();
        zfText.setTitle(floor.getFloorTitle());
        zfText.setUserId(userId);
        zfText.setEnterpriseId(enterpriseId);
        zfText.setFloorId(floorId);
        boolean saveZfText = cardZfFloorTextService.save(zfText);
        if (saveFloor && saveZfText) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addClusterFloor(CardEnterpriseFloorClusterDto clusterFloor) {
        String userId = clusterFloor.getUserId();
        CardEnterpriseFloor floor = clusterFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean save = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        List<CardClusterDetailInfo> clusterDetailInfoList = clusterFloor.getClusterFloorList();
        if (clusterDetailInfoList != null && clusterDetailInfoList.size() > 0) {
            List<CardClusterInfo> clusterInfoList = clusterDetailInfoList.stream()
                    .map(i -> {
                        CardClusterInfo cardClusterInfo = new CardClusterInfo();
                        BeanUtils.copyProperties(i, cardClusterInfo);
                        cardClusterInfo.setEnterpriseId(enterpriseId);
                        cardClusterInfo.setFloorId(floorId);
                        cardClusterInfo.setBaseCreateBy(userId);
                        cardClusterInfo.setBaseCreateTime(new Date());
                        return cardClusterInfo;
                    }).collect(Collectors.toList());
            boolean saveBatch = cardClusterInfoService.saveBatch(clusterInfoList);
            if (! saveBatch) {
                save = false;
            }
            for (CardClusterDetailInfo clusterDetailInfo : clusterDetailInfoList) {
                List<CardClusterEnterpriseBinding> enterpriseInfo = clusterDetailInfo.getEnterpriseInfo();
                if (enterpriseInfo != null && enterpriseInfo.size() > 0) {
                    List<CardClusterEnterpriseBinding> clusterEnterpriseBindingList = enterpriseInfo.stream()
                            .peek(k -> {
                                int index = clusterDetailInfoList.indexOf(clusterDetailInfo);
                                String clusterId = clusterInfoList.get(index).getBaseId();
                                k.setClusterId(clusterId);
                                k.setBaseCreateBy(userId);
                                k.setBaseCreateTime(new Date());
                            }).collect(Collectors.toList());
                    boolean saveBindingBatch = cardClusterEnterpriseBindingService.saveBatch(clusterEnterpriseBindingList);
                    if (! saveBindingBatch) {
                        save = false;
                    }
                }
            }
        }
        if (save) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addZfFileFloor(CardEnterpriseFloorZfFileDto zfDtoFloor) {
        String userId = zfDtoFloor.getUserId();
        CardEnterpriseFloor floor = zfDtoFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        Integer cardEnterpriseFloorSort = this.getFloorSort(enterpriseId);
        floor.setSort(cardEnterpriseFloorSort + 1);
        floor.setCreator(userId);
        boolean save = this.save(floor);
        String floorId = floor.getBaseId();
        saveFloorDept(floorId, floor.getDeptId());
        List<CardZfFile> zfFileFloors = zfDtoFloor.getZfFileFloorList();
        if (zfFileFloors != null && zfFileFloors.size() > 0) {
            List<CardZfFile> zfFileList = zfFileFloors.stream().peek(i -> {
                i.setBaseCreateBy(userId);
                //该字段设置为当前登录人的ID，并非设置出错
                i.setCardId(userId);
                i.setBaseCreateTime(new Date());
                i.setEnterpriseId(enterpriseId);
                i.setFloorId(floorId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardZfFileService.saveBatch(zfFileList);
            if (! saveBatch) {
                save = false;
            }
        }
        if (save) {
            return Message.success("新增成功");
        } else {
            return Message.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateMediaFloor(CardEnterpriseFloorMediaDto mediaFloor) {
        String userId = mediaFloor.getUserId();
        CardEnterpriseFloor floor = mediaFloor.getFloor();
        floor.setUpdateBy(userId);
        boolean update = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        cardEnterpriseFloorMediaService.remove(new LambdaQueryWrapper<CardEnterpriseFloorMedia>().eq(CardEnterpriseFloorMedia::getFloorId, floorId));
        List<CardEnterpriseFloorMedia> mediaFloors = mediaFloor.getMediaFloorList();
        if (mediaFloors != null && mediaFloors.size() > 0) {
            List<CardEnterpriseFloorMedia> mediaFloorList = mediaFloor.getMediaFloorList().stream().peek(i -> {
                i.setType(floor.getType());
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardEnterpriseFloorMediaService.saveBatch(mediaFloorList);
            if (! saveBatch) {
                update = false;
            }
        }
        if (update) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateAttachFloor(CardEnterpriseFloorAttachDto attachFloor) {
        String userId = attachFloor.getUserId();
        CardEnterpriseFloor floor = attachFloor.getFloor();
        floor.setUpdateBy(userId);
        boolean update = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        cardEnterpriseFloorAttachService.remove(new LambdaQueryWrapper<CardEnterpriseFloorAttach>().eq(CardEnterpriseFloorAttach::getFloorId, floorId));
        List<CardEnterpriseFloorAttach> attachFloors = attachFloor.getAttachFloorList();
        if (attachFloors != null && attachFloors.size() > 0) {
            List<CardEnterpriseFloorAttach> attachFloorList = attachFloor.getAttachFloorList().stream().peek(i -> {
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardEnterpriseFloorAttachService.saveBatch(attachFloorList);
            if (! saveBatch) {
                update = false;
            }
        }
        if (update) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateQualificationFloor(CardEnterpriseFloorQualificationDto qualificationFloor) {
        String userId = qualificationFloor.getUserId();
        CardEnterpriseFloor floor = qualificationFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        floor.setUpdateBy(userId);
        boolean update = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        luLianQualificationService.remove(new LambdaQueryWrapper<LuLianQualification>().eq(LuLianQualification::getFloorId, floorId));
        List<LuLianQualification> qualificationFloors = qualificationFloor.getQualificationFloorList();
        if (qualificationFloors != null && qualificationFloors.size() > 0) {
            List<LuLianQualification> qualificationFloorList = qualificationFloor.getQualificationFloorList().stream().peek(i -> {
                i.setEnterpriseId(enterpriseId);
                i.setType("1");
                i.setFloorId(floorId);
                i.setCreator(userId);
            }).collect(Collectors.toList());
            boolean saveBatch = luLianQualificationService.saveBatch(qualificationFloorList);
            if (! saveBatch) {
                update = false;
            }
        }
        if (update) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateRichTextFloor(CardEnterpriseFloorRichTextDto richTextFloor) {
        String userId = richTextFloor.getUserId();
        CardEnterpriseFloor floor = richTextFloor.getFloor();
        floor.setUpdateBy(userId);
        boolean updateFloor = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        CardRichText richText = richTextFloor.getRichTextFloor();
        richText.setTitle(floor.getFloorTitle());
        richText.setUpdateBy(userId);
        boolean updateRichText = cardRichTextService.updateById(richText);
        if (updateFloor && updateRichText) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateZfTextFloor(CardEnterpriseFloorZfTextDto zfTextFloor) {
        String userId = zfTextFloor.getUserId();
        CardEnterpriseFloor floor = zfTextFloor.getFloor();
        floor.setUpdateBy(userId);
        boolean updateFloor = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        CardZfFloorText zfText = zfTextFloor.getZfTextFloor();
        zfText.setTitle(floor.getFloorTitle());
        zfText.setUpdateBy(userId);
        boolean updateZfText = cardZfFloorTextService.updateById(zfText);
        if (updateFloor && updateZfText) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateClusterFloor(CardEnterpriseFloorClusterDto clusterFloor) {
        String userId = clusterFloor.getUserId();
        CardEnterpriseFloor floor = clusterFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        floor.setUpdateBy(userId);
        boolean update = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        List<CardClusterInfo> clusterInfos = cardClusterInfoService.list(new LambdaQueryWrapper<CardClusterInfo>().select(CardClusterInfo::getBaseId).eq(CardClusterInfo::getFloorId, floorId));
        for (CardClusterInfo clusterInfo : clusterInfos) {
            cardClusterEnterpriseBindingService.remove(new LambdaQueryWrapper<CardClusterEnterpriseBinding>().eq(CardClusterEnterpriseBinding::getClusterId, clusterInfo.getBaseId()));
        }
        cardClusterInfoService.remove(new LambdaQueryWrapper<CardClusterInfo>().eq(CardClusterInfo::getFloorId, floorId));
        List<CardClusterDetailInfo> clusterDetailInfoList = clusterFloor.getClusterFloorList();
        if (clusterDetailInfoList != null && clusterDetailInfoList.size() > 0) {
            List<CardClusterInfo> clusterInfoList = clusterDetailInfoList.stream()
                    .map(i -> {
                        CardClusterInfo cardClusterInfo = new CardClusterInfo();
                        BeanUtils.copyProperties(i, cardClusterInfo);
                        cardClusterInfo.setEnterpriseId(enterpriseId);
                        cardClusterInfo.setFloorId(floorId);
                        cardClusterInfo.setBaseCreateBy(userId);
                        cardClusterInfo.setBaseCreateTime(new Date());
                        return cardClusterInfo;
                    }).collect(Collectors.toList());
            boolean saveBatch = cardClusterInfoService.saveBatch(clusterInfoList);
            if (! saveBatch) {
                update = false;
            }
            for (CardClusterDetailInfo clusterDetailInfo : clusterDetailInfoList) {
                List<CardClusterEnterpriseBinding> enterpriseInfo = clusterDetailInfo.getEnterpriseInfo();
                if (enterpriseInfo != null && enterpriseInfo.size() > 0) {
                    List<CardClusterEnterpriseBinding> clusterEnterpriseBindingList = enterpriseInfo.stream()
                            .peek(k -> {
                                int index = clusterDetailInfoList.indexOf(clusterDetailInfo);
                                String clusterId = clusterInfoList.get(index).getBaseId();
                                k.setClusterId(clusterId);
                                k.setBaseCreateBy(userId);
                                k.setBaseCreateTime(new Date());
                            }).collect(Collectors.toList());
                    boolean saveBindingBatch = cardClusterEnterpriseBindingService.saveBatch(clusterEnterpriseBindingList);
                    if (! saveBindingBatch) {
                        update = false;
                    }
                }
            }
        }
        if (update) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateZfFileFloor(CardEnterpriseFloorZfFileDto zfFileFloor) {
        String userId = zfFileFloor.getUserId();
        CardEnterpriseFloor floor = zfFileFloor.getFloor();
        String enterpriseId = floor.getEnterpriseId();
        floor.setUpdateBy(userId);
        boolean update = this.updateById(floor);
        String floorId = floor.getBaseId();
        updateFloorDept(floorId, floor.getDeptId());
        cardZfFileService.remove(new LambdaQueryWrapper<CardZfFile>().eq(CardZfFile::getFloorId, floorId));
        List<CardZfFile> zfFileFloors = zfFileFloor.getZfFileFloorList();
        if (zfFileFloors != null && zfFileFloors.size() > 0) {
            List<CardZfFile> zfFileList = zfFileFloors.stream().peek(i -> {
                i.setBaseCreateBy(userId);
                //该字段设置为当前登录人的ID，并非设置出错
                i.setCardId(userId);
                i.setBaseCreateTime(new Date());
                i.setEnterpriseId(enterpriseId);
                i.setFloorId(floorId);
            }).collect(Collectors.toList());
            boolean saveBatch = cardZfFileService.saveBatch(zfFileList);
            if (! saveBatch) {
                update = false;
            }
        }
        if (update) {
            return Message.success("更新成功");
        } else {
            return Message.success("更新失败");
        }
    }

    /**
     * 批量保存楼层与部门间的关联关系
     */
    private void saveFloorDept(String floorId, String deptIds) {
        List<String> deptList = Arrays.asList(deptIds.split(","));
        List<CardEnterpriseFloorDept> dataModel = new ArrayList<>();
        for (String deptId : deptList) {
            CardEnterpriseFloorDept floorDept = CardEnterpriseFloorDept.builder().floorId(floorId).deptId(deptId).build();
            dataModel.add(floorDept);
        }
        cardEnterpriseFloorDeptService.saveBatch(dataModel);
    }

    /**
     * 批量更新楼层与部门间的关联关系
     */
    private void updateFloorDept(String floorId, String deptIds) {
        cardEnterpriseFloorDeptService.remove(new LambdaQueryWrapper<CardEnterpriseFloorDept>().eq(CardEnterpriseFloorDept::getFloorId, floorId));
        saveFloorDept(floorId, deptIds);
    }

    @Override
    public Message getMediaFloorDetails(String baseId) {
        CardEnterpriseFloorMediaVo cardEnterpriseFloorMediaVo = new CardEnterpriseFloorMediaVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<CardEnterpriseFloorMedia> mediaFloorList = cardEnterpriseFloorMediaService.list(new LambdaQueryWrapper<CardEnterpriseFloorMedia>().eq(CardEnterpriseFloorMedia::getFloorId, baseId).orderByAsc(CardEnterpriseFloorMedia::getMediaSort));
        cardEnterpriseFloorMediaVo.setFloor(floor);
        cardEnterpriseFloorMediaVo.setMediaFloorList(mediaFloorList);
        return Message.success(cardEnterpriseFloorMediaVo);
    }

    @Override
    public Message getAttachFloorDetails(String baseId) {
        CardEnterpriseFloorAttachVo cardEnterpriseFloorAttachVo = new CardEnterpriseFloorAttachVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<CardEnterpriseFloorAttach> attachFloorList = cardEnterpriseFloorAttachService.list(new LambdaQueryWrapper<CardEnterpriseFloorAttach>().eq(CardEnterpriseFloorAttach::getFloorId, baseId).orderByAsc(CardEnterpriseFloorAttach::getAttachSort));
        cardEnterpriseFloorAttachVo.setFloor(floor);
        cardEnterpriseFloorAttachVo.setAttachFloorList(attachFloorList);
        return Message.success(cardEnterpriseFloorAttachVo);
    }

    @Override
    public Message getLinkFloorDetails(String baseId) {
        CardEnterpriseFloorLinkVo cardEnterpriseFloorLinkVo = new CardEnterpriseFloorLinkVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<CardEnterpriseFloorLink> linkFloorList = cardEnterpriseFloorLinkService.list(new LambdaQueryWrapper<CardEnterpriseFloorLink>().eq(CardEnterpriseFloorLink::getFloorId, baseId).orderByAsc(CardEnterpriseFloorLink::getLinkSort));
        cardEnterpriseFloorLinkVo.setFloor(floor);
        cardEnterpriseFloorLinkVo.setLinkFloorList(linkFloorList);
        return Message.success(cardEnterpriseFloorLinkVo);
    }

    @Override
    public Message getQualificationFloorDetails(String baseId) {
        CardEnterpriseFloorQualificationVo cardEnterpriseFloorQualificationVo = new CardEnterpriseFloorQualificationVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<LuLianQualification> qualificationFloorList = luLianQualificationService.list(new LambdaQueryWrapper<LuLianQualification>().eq(LuLianQualification::getFloorId, baseId).orderByAsc(LuLianQualification::getSort));
        cardEnterpriseFloorQualificationVo.setFloor(floor);
        cardEnterpriseFloorQualificationVo.setQualificationFloorList(qualificationFloorList);
        return Message.success(cardEnterpriseFloorQualificationVo);
    }

    @Override
    public Message getRichTextFloorDetails(String baseId) {
        CardEnterpriseFloorRichTextVo cardEnterpriseFloorRichTextVo = new CardEnterpriseFloorRichTextVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        CardRichText richText = cardRichTextService.getOne(new LambdaQueryWrapper<CardRichText>().eq(CardRichText::getFloorId, baseId));
        cardEnterpriseFloorRichTextVo.setFloor(floor);
        cardEnterpriseFloorRichTextVo.setRichTextFloor(richText);
        return Message.success(cardEnterpriseFloorRichTextVo);
    }

    @Override
    public Message getZfTextFloorDetails(String baseId) {
        CardEnterpriseFloorZfTextVo cardEnterpriseFloorZfTextVo = new CardEnterpriseFloorZfTextVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        CardZfFloorText zfText = cardZfFloorTextService.getOne(new LambdaQueryWrapper<CardZfFloorText>().eq(CardZfFloorText::getFloorId, baseId));
        cardEnterpriseFloorZfTextVo.setFloor(floor);
        cardEnterpriseFloorZfTextVo.setZfTextFloor(zfText);
        return Message.success(cardEnterpriseFloorZfTextVo);
    }

    @Override
    public Message getClusterFloorDetails(String baseId) {
        CardEnterpriseFloorClusterVo cardEnterpriseFloorClusterVo = new CardEnterpriseFloorClusterVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<CardClusterDetailInfo> clusterInfoList = cardClusterInfoService.getList(baseId);
        cardEnterpriseFloorClusterVo.setFloor(floor);
        cardEnterpriseFloorClusterVo.setClusterFloorList(clusterInfoList);
        return Message.success(cardEnterpriseFloorClusterVo);
    }

    @Override
    public Message getZfFileFloorDetails(String baseId) {
        CardEnterpriseFloorZfFileVo cardEnterpriseFloorZfFileVo = new CardEnterpriseFloorZfFileVo();
        CardEnterpriseFloorVo floor = this.getFloorById(baseId);
        List<CardZfFile> zfFileList = cardZfFileService.list(new LambdaQueryWrapper<CardZfFile>().eq(CardZfFile::getFloorId, baseId));
        cardEnterpriseFloorZfFileVo.setFloor(floor);
        cardEnterpriseFloorZfFileVo.setZfFileFloorList(zfFileList);
        return Message.success(cardEnterpriseFloorZfFileVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message removeFloor(String enterpriseId, String baseId, String type, Integer sort) {
        //根据类型删除该楼层中的内容
        if (StrUtil.equals(type, "4") || StrUtil.equals(type, "7")) {
            //图片类和视频类
            cardEnterpriseFloorMediaService.remove(new LambdaQueryWrapper<CardEnterpriseFloorMedia>().eq(CardEnterpriseFloorMedia::getFloorId, baseId));
        } else if (StrUtil.equals(type, "5")) {
            //附件类
            cardEnterpriseFloorAttachService.remove(new LambdaQueryWrapper<CardEnterpriseFloorAttach>().eq(CardEnterpriseFloorAttach::getFloorId, baseId));
        } else if (StrUtil.equals(type, "6")) {
            //链接类
            cardEnterpriseFloorLinkService.remove(new LambdaQueryWrapper<CardEnterpriseFloorLink>().eq(CardEnterpriseFloorLink::getFloorId, baseId));
        } else if (StrUtil.equals(type, "1")) {
            //资质类
            luLianQualificationService.remove(new LambdaQueryWrapper<LuLianQualification>().eq(LuLianQualification::getFloorId, baseId));
        } else if (StrUtil.equals(type, "3") || StrUtil.equals(type, "11")) {
            //图文类
            cardRichTextService.remove(new LambdaQueryWrapper<CardRichText>().eq(CardRichText::getFloorId, baseId));
        } else if (StrUtil.equals(type, "8")) {
            //集群类（政府）
            LambdaQueryWrapper<CardClusterInfo> clusterInfoQueryWrapper = new LambdaQueryWrapper<CardClusterInfo>().eq(CardClusterInfo::getFloorId, baseId);
            List<CardClusterInfo> clusterInfoList = cardClusterInfoService.list(clusterInfoQueryWrapper);
            List<String> clusterInfoIds = clusterInfoList.stream().map(CardClusterInfo::getBaseId).collect(Collectors.toList());
            cardClusterEnterpriseBindingService.remove(new LambdaQueryWrapper<CardClusterEnterpriseBinding>().in(CardClusterEnterpriseBinding::getClusterId, clusterInfoIds));
            cardClusterInfoService.remove(clusterInfoQueryWrapper);
        } else if (StrUtil.equals(type, "9") || StrUtil.equals(type, "12")) {
            //图文类（政府）
            cardZfFloorTextService.remove(new LambdaQueryWrapper<CardZfFloorText>().eq(CardZfFloorText::getFloorId, baseId));
        } else if (StrUtil.equals(type, "10")) {
            //附件类（政府）
            cardZfFileService.remove(new LambdaQueryWrapper<CardZfFile>().eq(CardZfFile::getFloorId, baseId));
        }
        //删除该楼层
        boolean remove = this.removeById(baseId);
        //删除楼层与部门间的关联关系
        cardEnterpriseFloorDeptService.remove(new LambdaQueryWrapper<CardEnterpriseFloorDept>().eq(CardEnterpriseFloorDept::getFloorId, baseId));
        //对剩下的楼层进行重新排序
        List<CardEnterpriseFloor> floorList = this.list(new LambdaQueryWrapper<CardEnterpriseFloor>()
                .select(CardEnterpriseFloor::getBaseId, CardEnterpriseFloor::getSort)
                .eq(CardEnterpriseFloor::getEnterpriseId, enterpriseId)
                .gt(CardEnterpriseFloor::getSort, sort)
                .orderByAsc());
        List<CardEnterpriseFloor> newFloorList = floorList.stream().peek(i -> i.setSort(i.getSort() - 1)).collect(Collectors.toList());
        boolean updateBatch = this.updateBatchById(newFloorList);
        if (remove && updateBatch) {
            return Message.success("删除成功");
        } else {
            return Message.success("删除失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateFloor(CardEnterpriseFloorDto floorDto) {
        CardEnterpriseFloor cardEnterpriseFloor = new CardEnterpriseFloor();
        BeanUtils.copyProperties(floorDto, cardEnterpriseFloor);
        cardEnterpriseFloor.setUpdateBy(floorDto.getUserId());
        int update = cardEnterpriseFloorMapper.updateById(cardEnterpriseFloor);
        //对于企业的图文类楼层，在修改楼层 isShow 值时需要将对应值同步到企业图文表中的记录
        if (StrUtil.equals(floorDto.getType(), "3") || StrUtil.equals(floorDto.getType(), "11")) {
            boolean updateRichTextIsShow = cardRichTextService.update(new LambdaUpdateWrapper<CardRichText>().eq(CardRichText::getFloorId, floorDto.getBaseId())
                    //由于 CardRichText 中 isShow 值的意义与楼层的 isShow 值的意义相反，因此需要颠倒赋值
                    .set(CardRichText::getIsShow, StrUtil.equals(floorDto.getIsShow(), "0") ? "1" : "0"));
            if (! updateRichTextIsShow) {
                update = 0;
            }
        }
        //对于政府的图文类楼层，在修改楼层 isShow 值时需要将对应值同步到政府图文表中的记录
        if (StrUtil.equals(floorDto.getType(), "9") || StrUtil.equals(floorDto.getType(), "12")) {
            boolean updateZfTextIsShow = cardZfFloorTextService.update(new LambdaUpdateWrapper<CardZfFloorText>().eq(CardZfFloorText::getFloorId, floorDto.getBaseId())
                    //由于 CardRichText 中 isShow 值的意义与楼层的 isShow 值的意义相反，因此需要颠倒赋值
                    .set(CardZfFloorText::getIsShow, StrUtil.equals(floorDto.getIsShow(), "0") ? "1" : "0"));
            if (! updateZfTextIsShow) {
                update = 0;
            }
        }
        if (update > 0) {
            return Message.success("更新成功");
        } else {
            return Message.fail("更新失败");
        }
    }
}